from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from lazypredict.Supervised import LazyClassifier
import pickle
import pandas as pd

with open('X.pkl', 'rb') as file:  
    X = pickle.load(file)  
  
with open('y.pkl', 'rb') as file:  
    y = pickle.load(file)  

if not isinstance(X, pd.DataFrame):
    X = pd.DataFrame(X)
if not isinstance(y, pd.DataFrame):
    y = pd.DataFrame(y)


# 将数据集分为训练集和测试集，测试集占比50%
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 使用LazyClassifier自动选择和评估各种分类器
clf = LazyClassifier()
result, _ = clf.fit(X_train, X_test, y_train, y_test)
print(result)

# 获取准确率分数最高的模型
best_Accuracy = result['Accuracy'].idxmax()  # 获取F1分数最高行的索引值，即：模型名称
print("\nAccuracy最高的模型是: ", best_Accuracy)

#保存准确率最高的模型
best_model = clf.models[best_Accuracy]  # 根据模型名称，从模型字典中获取模型对象

with open('best_Accuracy_model.pkl', 'wb') as f:
    pickle.dump(best_model,f)

